#罗马数字转整数
s = "MCMXCIV"

s = s.replace('IV', 'a')
s = s.replace('IX', 'b')
s = s.replace('XL', 'c')
s = s.replace('XC', 'd')
s = s.replace('CD', 'e')
s = s.replace('CM', 'f')
print(s)
sum = 0

for i in s:
    if i == 'I':
        sum += 1
    elif i == 'V':
        sum += 5
    elif i == 'X':
        sum += 10
    elif i == 'L':
        sum += 50
    elif i == 'C':
        sum += 100
    elif i == 'D':
        sum += 500
    elif i == 'M':
        sum += 1000
    elif i == 'a':
        sum += 4
    elif i == 'b':
        sum += 9
    elif i == 'c':
        sum += 40
    elif i == 'd':
        sum += 90
    elif i == 'e':
        sum += 400
    elif i == 'f':
        sum += 900

print(sum)


#赎金信
class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        magazine = list(magazine)
        for i in ransomNote:
            if i in magazine:
                magazine.remove(i)
            else:
                return False
        return True


#同构字符串
class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        return len(set(s)) == len(set(t)) == len(set(zip(s,t)))


#单词规律
word1 = "abba"
word2 = "dog cat cat dog"
word2 = word2.split(' ')
print(list(map(word1.index, word1)) == list(map(word2.index, word2)))


#有效的字母异位词
from collections import Counter
s = 'abbccc'
d = 'cccbba'
print(Counter(s) == Counter(d))


#两数之和
nums = [3,2,4]
target = 6

dic = {}

for i, j in enumerate(nums):
    if target - j in dic.keys():
        print(i, dic.get(target-j))
    else:
        dic[j] = i
else:
    print("f")


#快乐数
class Solution:
    def isHappy(self, n: int) -> bool:
        # return self.isHappy(sum(int(i) ** 2 for i in str(n))) if n > 4 else n == 1
        su = 0
        j = 0
        while n > 4:
            for i in str(n):
                i = int(i)
                j = i ** 2
                su += j
                i += 1
            n = su
            su = 0
            print(n)
        else:
            if n == 1:
                return True
            else:
                return False


#存在重复元素2
class Solution:
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
        i = 0
        s = set()
        while i < len(nums):
            if nums[i] in s:
                return True
            else:
                s.add(nums[i])
                if i >= k:
                    s.remove(nums[i - k])
            i += 1
        return False



#汇总区间
nums = [0, 1, 2, 4, 5, 7]
i, j = 0, 0
res = []
while j < len(nums):
    while j + 1 < len(nums) and nums[j] + 1 == nums[j + 1]:
        j += 1
    if j == i:
        res.append(f"{nums[j]}")
    else:
        res.append(f"{nums[i]}->{nums[j]}")
    i = j = j + 1
print(res)


#有效的括号
class Solution:
    def isValid(self, s: str) -> bool:
        dic = {'(':')', '[':']', '{':'}','?':'?'}
        s1 = ['?']
        for i in s:
            if i in dic: s1.append(i)
            elif dic[s1.pop()] != i: return False
        return len(s1) == 1